package com.kotiln.lgc.sunnyweather.logic.network

import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

/**
 *@fileName ServiceCreator
 *@date2020/11/26  16:30
 *@author ServiceCreator$
 *@describe TODO 单例
 *@desc  构建器
 */
object ServiceCreator {
    //根路径
    private const val BASE_URL="https://api.caiyunapp.com/"
    private val client:OkHttpClient
    init {
        val httpLoggingInterceptor = HttpLoggingInterceptor()
        httpLoggingInterceptor.level =HttpLoggingInterceptor.Level.BODY
         client = OkHttpClient.Builder()//构造者设计模式
            .connectTimeout(10, TimeUnit.SECONDS)//连接超时时间
            .readTimeout(10, TimeUnit.SECONDS)//读取超时
            .writeTimeout(10, TimeUnit.SECONDS)//写超时,也就是请求超时
            .addInterceptor(httpLoggingInterceptor) //拦截器 LoggingInterceptor()自定义拦截器
            .build()
    }
    //Retrofit构建器
    private val retrofit=Retrofit.Builder().client(client).baseUrl(BASE_URL).addConverterFactory(GsonConverterFactory.create()).build()
    fun <T> create(serviceClass:Class<T>):T=retrofit.create(serviceClass)
    //泛型实化
    inline fun<reified T>create():T=create(T::class.java)
}